package com.drug.wms.service.impl;

import com.drug.common.enums.BusinessSeqEnum;
import com.drug.common.utils.DateUtils;
import com.drug.common.utils.uuid.IdUtils;
import com.drug.wms.domain.BusContactsUnit;
import com.drug.wms.mapper.BusContactsUnitMapper;
import com.drug.wms.service.GenerateSequence;
import com.drug.wms.service.IBusContactsUnitService;
import com.drug.wms.vo.BusContactsUnitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 往来单位Service业务层处理
 *
 * @author wjZhao
 * @date 2021-11-13
 */
@Service
public class BusContactsUnitServiceImpl implements IBusContactsUnitService {
    @Autowired
    private BusContactsUnitMapper busContactsUnitMapper;
    @Autowired
    private BusFileServiceImpl busFileService;

    /**
     * 查询往来单位
     *
     * @param id 往来单位主键
     * @return 往来单位
     */
    @Override
    public BusContactsUnitVo selectBusContactsUnitById(String id) {
        BusContactsUnitVo busContactsUnitVo = new BusContactsUnitVo();
        busContactsUnitVo.setBusContactsUnit(busContactsUnitMapper.selectBusContactsUnitById(id));
        busContactsUnitVo.setBusFileList(busFileService.listByBusId(id));
        return busContactsUnitVo;
    }

    /**
     * 查询往来单位列表
     *
     * @param busContactsUnit 往来单位
     * @return 往来单位
     */
    @Override
    public List<BusContactsUnit> selectBusContactsUnitList(BusContactsUnit busContactsUnit) {
        return busContactsUnitMapper.selectBusContactsUnitList(busContactsUnit);
    }

    /**
     * 新增往来单位
     *
     * @param busContactsUnitVo 往来单位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusContactsUnit(BusContactsUnitVo busContactsUnitVo) {
        BusContactsUnit busContactsUnit = busContactsUnitVo.getBusContactsUnit();
        String busId = IdUtils.fastUUID();
        busContactsUnit.setId(busId);
        busContactsUnit.setUnitNo(GenerateSequence.genSeqNo(BusinessSeqEnum.CU.getCode()));
        busContactsUnit.setCreateTime(DateUtils.getNowDate());

        busFileService.batchInsertBusFile(busContactsUnitVo.getBusFileList(), busId);

        return busContactsUnitMapper.insertBusContactsUnit(busContactsUnit);
    }

    /**
     * 修改往来单位
     *
     * @param busContactsUnitVo 往来单位
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusContactsUnit(BusContactsUnitVo busContactsUnitVo) {
        BusContactsUnit busContactsUnit = busContactsUnitVo.getBusContactsUnit();
        busContactsUnit.setUpdateTime(DateUtils.getNowDate());

        busFileService.deleteBusFileByBusIds(new String[]{busContactsUnit.getId()});

        busFileService.batchInsertBusFile(busContactsUnitVo.getBusFileList(), busContactsUnit.getId());

        return busContactsUnitMapper.updateBusContactsUnit(busContactsUnit);
    }

    /**
     * 批量删除往来单位
     *
     * @param ids 需要删除的往来单位主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusContactsUnitByIds(String[] ids) {
        busFileService.deleteBusFileByBusIds(ids);
        return busContactsUnitMapper.deleteBusContactsUnitByIds(ids);
    }

    /**
     * 删除往来单位信息
     *
     * @param id 往来单位主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusContactsUnitById(String id) {
        return busContactsUnitMapper.deleteBusContactsUnitById(id);
    }
}
